package itheima.com.Day15.socketDemo08;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyThread implements Runnable {
    private Socket acceptsocket;

    public MyThread(Socket accept) {
        this.acceptsocket=accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos=null;
        try {
            BufferedInputStream bis = new BufferedInputStream(acceptsocket.getInputStream());
             bos = new BufferedOutputStream(new FileOutputStream("Day15\\src\\Server" + UUID.randomUUID().toString() + ".1jpg"));

            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(acceptsocket.getOutputStream()));
            bw.write("上传成功啦");
            bw.flush();
            bw.close();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(acceptsocket!=null){
                try {
                    acceptsocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
